package ggze.mps.home.controller;


import ggze.esp.dao.inter.NavigationDao;
import ggze.esp.entity.Navigation;
import ggze.mps.home.dao.inter.UserDao;
import ggze.mps.home.entity.User;
import ggze.loader.Loading;
import ggze.loader.Storage;
import ggze.tool.BeanToJson;
import ggze.tool.JwtToken;
import ggze.tool.PropertiesUtils;
import ggze.tool.ReturnBody;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("NavigationController")
public class NavigationController {
    @Autowired
    private NavigationDao navigationDao;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserDao userDao;
    @Autowired
    private HttpServletResponse response;

    @RequestMapping("login")
    @ResponseBody
    public String login() throws UnsupportedEncodingException {
        JSONObject jsonObject=JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody=new ReturnBody();
        String name=jsonObject.getString("name");
        String pass=jsonObject.getString("pass");

        Map<String, String> x = new PropertiesUtils(Storage.getObj().getConfigPath()).getNodes();
        String admin=x.get("admin");
        String ps=x.get("pass");
        if (!name.equals(admin)){
            returnBody.setCode("1");
            returnBody.setTitle("账号错误");
            return BeanToJson.toJsonObjectString(returnBody);
        }
        if(!pass.equals(ps)){
            returnBody.setCode("1");
            returnBody.setTitle("密码错误");
            return BeanToJson.toJsonObjectString(returnBody);
        }
        returnBody.setCode("0");
        returnBody.setTitle("成功");
        Map<String, String> bodymap = new LinkedHashMap<String, String>();
        bodymap.put("token", JwtToken.createToken(admin+pass));
        bodymap.put("user", admin);
        returnBody.setData(BeanToJson.toJsonObjectString(bodymap));
        return BeanToJson.toJsonObjectString(returnBody);
    }
    @RequestMapping("loginSQL")
    @ResponseBody
    public String loginSQL() throws UnsupportedEncodingException {
        JSONObject jsonObject=JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody=new ReturnBody();
        String name=jsonObject.getString("name");
        String pass=jsonObject.getString("pass");

        User user= userDao.isSelect("ipone",name);
        if(user==null||"".equals(user)){
            user=userDao.isSelect("emil",name);
        }
        if(user==null||"".equals(user)){
            returnBody.setCode("1");
            returnBody.setTitle("用户名或密码不正确");
            return BeanToJson.toJsonObjectString(returnBody);
        }
        String ps=user.getPassWord();
        if(!pass.equals(ps)){
            returnBody.setCode("1");
            returnBody.setTitle("密码错误");
            return BeanToJson.toJsonObjectString(returnBody);
        }
        if("1".equals(user.getStatus())){
            returnBody.setCode("1");
            returnBody.setTitle("账户已被禁用");
            return BeanToJson.toJsonObjectString(returnBody);
        }
        returnBody.setCode("0");
        returnBody.setTitle("成功");
        Map<String, String> bodymap = new LinkedHashMap<String, String>();
        bodymap.put("token", JwtToken.createToken(name+pass));
        bodymap.put("user", BeanToJson.toJsonObjectString(user));
        returnBody.setData(BeanToJson.toJsonObjectString(bodymap));
        return BeanToJson.toJsonObjectString(returnBody);
    }
    @RequestMapping("get")
    @ResponseBody
    public String get() throws UnsupportedEncodingException {
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {
            int begin = Integer.parseInt(jsonObject.getString("begin"));
            int end = Integer.parseInt(jsonObject.getString("end"));
            String zdname = jsonObject.getString("zdname");
            String zdval = jsonObject.getString("zdval");
            if ("1".equals(zdval)) {
                List<Navigation> entityLIST = navigationDao.paging(zdname, zdval, begin, end);
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            } else {
                List<Navigation> entityLIST = navigationDao.customs("select * from Navigation where " + zdname + "  like '%" + zdval + "%'");
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            }
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }
    @RequestMapping("seach")
    @ResponseBody
    public String seach() {
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {
            String uuid = jsonObject.getString("uuid");
            String navig = jsonObject.getString("navig");
            String roleCode = jsonObject.getString("roleCode");
            List<Map<String,String>> res = navigationDao.getNavigationByRolecode(roleCode,uuid,navig);
            returnBody.setCode("0");
            returnBody.setData(BeanToJson.toJsonArrayString(res));
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }
}
